import request from './request'
import { useAuthStore } from '@/stores/auth'

export const messageApi = {
  // 发送消息
  sendMessage(data) {
    const authStore = useAuthStore()
    return request.post('/messages/send', data, {
      params: { username: authStore.user.username }
    })
  },

  // 获取聊天消息
  getChatMessages(otherUserId, page = 0, size = 20) {
    const authStore = useAuthStore()
    return request.get(`/messages/chat/${otherUserId}`, {
      params: { 
        username: authStore.user.username,
        page,
        size
      }
    })
  },

  // 获取未读消息
  getUnreadMessages() {
    const authStore = useAuthStore()
    return request.get('/messages/unread', {
      params: { username: authStore.user.username }
    })
  },

  // 标记消息为已读
  markAsRead(messageId) {
    return request.put(`/messages/${messageId}/read`)
  },

  // 撤回消息
  recallMessage(messageId) {
    const authStore = useAuthStore()
    return request.put(`/messages/${messageId}/recall`, null, {
      params: { username: authStore.user.username }
    })
  },

  // 获取可撤回的消息
  getRecallableMessages() {
    const authStore = useAuthStore()
    return request.get('/messages/recallable', {
      params: { username: authStore.user.username }
    })
  },

  // 统计未读消息数量
  countUnreadMessages() {
    const authStore = useAuthStore()
    return request.get('/messages/unread/count', {
      params: { username: authStore.user.username }
    })
  }
} 